package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import butterknife.ButterKnife;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.entity.KnowledgeAnnounceList;
import com.isunland.managesystem.entity.KnowledgeAnnounceList.KnowledgeAnnounceDetail;
import com.isunland.managesystem.utils.MenuUtil;
import com.isunland.managesystem.widget.SingleLineViewNew;

/**
 * 知识公文详情
 *
 * @author wu
 */
public class KnowledgeAnnounceDetailFragment extends BaseFragment {

  public static final String EXTRA_VALUE = "com.isunland.managesystem.ui.EXTRA_VALUE";

  private SingleLineViewNew mtvAnnounceTypeAnnounceDetail;// 公文类别
  private String mType;
  private WebView mtvContentConstructAnnounceDetail;// 内容简述
  private String mConstruction;
  private SingleLineViewNew mtvKeyWordsDetailAnnounceDetail;// 关键字
  private String mkeyWords;
  private SingleLineViewNew mtvVisualRangeAnnounceDetail;// 可见范围
  private String mRange;
  private SingleLineViewNew mtvRegeditDepartmentAnnounceDetail;// 登记部门
  private String mDepartment;
  private SingleLineViewNew mtvRegisterAnnounceDetail;// 登记人
  private String mRegister;
  private SingleLineViewNew mtvRegisterTimeAnnounceDetail;// 登记时间
  private String mRegistTime;
  private SingleLineViewNew mtvFilesAnnounceDetail;// 附件
  private String mFile;
  private KnowledgeAnnounceDetail mItem;

  public static Fragment newInstance(KnowledgeAnnounceDetail detail) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_VALUE, detail);
    KnowledgeAnnounceDetailFragment fragment = new KnowledgeAnnounceDetailFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 设置返回图标
    setTitleCustom(R.string.knowledgeAnnounceDtail);
    mItem = (KnowledgeAnnounceDetail) getArguments().getSerializable(EXTRA_VALUE);
    mItem = mItem == null ? new KnowledgeAnnounceList().new KnowledgeAnnounceDetail() : mItem;
    mType = mItem.getDocKindName();
    mConstruction = mItem.getDocContentDesc();
    mkeyWords = mItem.getDocKeyWords();
    mRange = mItem.getReadRangeName();
    mDepartment = mItem.getRegDeptName();
    mRegister = mItem.getRegStaffName();
    mRegistTime = mItem.getRegDate();
    mFile = mItem.getFilePath();
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_knowledge_detail, container, false);
    ButterKnife.bind(this, view);
    mtvAnnounceTypeAnnounceDetail =
        (SingleLineViewNew) view.findViewById(R.id.tv_announceType_announceDetail);
    mtvContentConstructAnnounceDetail =
        (WebView) view.findViewById(R.id.tv_contentConstruct_announceDetail);
    mtvKeyWordsDetailAnnounceDetail =
        (SingleLineViewNew) view.findViewById(R.id.tv_keyWordsDetail_announceDetail);
    mtvVisualRangeAnnounceDetail =
        (SingleLineViewNew) view.findViewById(R.id.tv_visualRange_announceDetail);
    mtvRegeditDepartmentAnnounceDetail =
        (SingleLineViewNew) view.findViewById(R.id.tv_regeditDepartment_announceDetail);
    mtvRegisterAnnounceDetail =
        (SingleLineViewNew) view.findViewById(R.id.tv_register_announceDetail);
    mtvRegisterTimeAnnounceDetail =
        (SingleLineViewNew) view.findViewById(R.id.tv_registerTime_announceDetail);
    mtvFilesAnnounceDetail = (SingleLineViewNew) view.findViewById(R.id.tv_files_announceDetail);
    initDetailData();
    mtvFilesAnnounceDetail.setOnClickContentListener(new OnClickListener() {
      @Override public void onClick(View view) {
        if (!TextUtils.isEmpty(mFile)) {
          ExtraDownLoadDialogFragment dialog = ExtraDownLoadDialogFragment.newInstance("", mFile);
          FragmentManager fm = getActivity().getSupportFragmentManager();
          dialog.show(fm, "");
        }
      }
    });
    return view;
  }

  private void initDetailData() {
    if (!TextUtils.isEmpty(mType)) {
      mtvAnnounceTypeAnnounceDetail.setTextContent(mType);
    }
    if (!TextUtils.isEmpty(mConstruction)) {
      mtvContentConstructAnnounceDetail.loadDataWithBaseURL(mConstruction, mConstruction,
          "text/html", "utf-8", null);
    }
    if (!TextUtils.isEmpty(mkeyWords)) {
      mtvKeyWordsDetailAnnounceDetail.setTextContent(mkeyWords);
    }
    if (!TextUtils.isEmpty(mRange)) {
      mtvVisualRangeAnnounceDetail.setTextContent(mRange);
    }
    if (!TextUtils.isEmpty(mDepartment)) {
      mtvRegeditDepartmentAnnounceDetail.setTextContent(mDepartment);
    }
    if (!TextUtils.isEmpty(mRegister)) {
      mtvRegisterAnnounceDetail.setTextContent(mRegister);
    }
    if (!TextUtils.isEmpty(mRegistTime)) {
      mtvRegisterTimeAnnounceDetail.setTextContent(mRegistTime);
    }

    if (!TextUtils.isEmpty(mFile)) {
      String fileName = mFile.substring(mFile.lastIndexOf("/") + 1, mFile.length());
      mtvFilesAnnounceDetail.setTextContent(fileName);
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:

        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    MenuUtil.addReadReceiptMenu(mActivity, menu, mItem.getReadReceipt(), mItem.getId());
  }
}
